package com.example.Holo.rework.dao.implementation.sqlite.android;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.Holo.rework.core.Friend;
import com.example.Holo.rework.dao.interfaces.IFriendDao;
import com.google.common.base.Supplier;

import java.util.List;

import static com.example.Holo.rework.dao.implementation.sqlite.android.Column.*;
import static com.example.Holo.rework.dao.implementation.sqlite.android.ColumnDescription.createColumn;
import static com.example.Holo.rework.dao.implementation.sqlite.android.Nullable.NOT_NULL;
import static com.example.Holo.rework.dao.implementation.sqlite.android.PrimaryKey.primaryKey;
import static java.util.Arrays.asList;

public class FriendDao extends GenericDao<Friend> implements IFriendDao {

    protected FriendDao(Context context, String databaseName, SQLiteDaoFactory daoFactory) {
        super(context, databaseName, daoFactory);
    }

    @Override
    protected String getTableName() {
        return "friends";
    }

    @Override
    protected List<ColumnDescription> getColumns() {
        return columnDescriptions;
    }

    @Override
    protected ContentValues getContentValues(Friend friend) {
        final ContentValues values = new ContentValues();
        values.put(NAME_COLUMN.name, friend.name);
        return values;
    }

    @Override
    protected Friend createFromCursor(Cursor cursor) {
        return new Friend(
                cursor.getLong(cursor.getColumnIndex(ID_COLUMN.name)),
                cursor.getString(cursor.getColumnIndex(NAME_COLUMN.name))
        );
    }

    @Override
    protected Column getIdColumnName() {
        return ID_COLUMN;
    }

    @Override
    public List<Friend> getAllFriends(final long eventId) {
        return withinCursor(new Supplier<Cursor>() {
            @Override
            public Cursor get() {
                final SQLiteDatabase database = getWritableDatabase();
                return database.query(getTableName(), getAllColumns(), EVENT_ID_COLUMN.name + " = " + eventId,
                        null, null, null, null);
            }
        }, cursorToArrayFunction);
    }

    @Override
    public long addFriend(long eventId, Friend friend) {
        final SQLiteDatabase database = getWritableDatabase();
        final ContentValues contentValues = getContentValues(friend);
        contentValues.put(EVENT_ID_COLUMN.name, eventId);
        return database.insert(getTableName(), null, contentValues);
    }

    @Override
    public int removeFriend(long eventId, long friendId) {
        final SQLiteDatabase database = getWritableDatabase();
        final String where = getIdColumnName() + " = " + friendId + " AND " + EVENT_ID_COLUMN.name + " = " + eventId;
        return database.delete(getTableName(), where, null);
    }

    @Override
    public int removeAllFriends(long eventId) {
        final SQLiteDatabase database = getWritableDatabase();
        final String where = EVENT_ID_COLUMN + " = " + eventId;
        return database.delete(getTableName(), where, null);
    }

    private final List<ColumnDescription> columnDescriptions = asList(
            createColumn(ID_COLUMN, primaryKey(true, NOT_NULL)),
            createColumn(EVENT_ID_COLUMN),
            createColumn(NAME_COLUMN)
    );

}
